<?php

declare(strict_types=1);

namespace app\api\model;

/**
 * 工具类模型
 */
class Tool extends Base
{

    /**
     * # 手机号加密
     * @param array|object $array
     * @return string
     */

    public static function encryptPhone(&$array)
    {

        if (isset($array['account'])) {
            $array['account'] = substr_replace($array['account'], '****', 3, 4);
        } else {
            foreach ($array as $key => $arr) {
                $array[$key]['account'] = substr_replace($arr['account'], '****', 3, 4);
            }
        }
    }

    /**
     *  # 获取键值组成的数组
     * @param array $arrays
     * @return false|object|null $ids
     */
    public static function getKeyArray($arrays, $key)
    {
        if (!is_array($arrays)) {
            return false;
        }
        if (count($arrays) == count($arrays, 1)) {
            return false;
        }
        $arr = array_column($arrays, $key);
        return $arr;
    }

    /**
     * # 合并数组
     * @param array $parentArrays $childArrays
     * @param string $pid $childName;
     * @return array
     */

    public static function mergeArray($parentArrays, $childArrays, $pid, $childName)
    {
        
        $data = $parentArrays;
        foreach ($parentArrays as $parentKey => $parentArray) {
            if (!isset($parentArray['id'])) {
                continue;
            }
            foreach ($childArrays as $childKey => $childArray) {
                if (!isset($childArray[$pid])) {
                    continue;
                }
                if ($childArray[$pid] == $parentArray['id']) {
                    $data[$parentKey][$childName][] = $childArrays[$childKey];
                    unset($childArrays[$childKey]);
                }
            }
        }
        return $data;
    }
}
